import axios from 'axios';

export function is_weixin() { 
    var ua = window.navigator.userAgent.toLowerCase(); 
    if (ua.match(/MicroMessenger/i) == 'micromessenger') { 
        return true;
    } else { 
        return false;
    } 
}

export function getUrlParam(paramname) {
    var regobject = new RegExp('(^|&)' + paramname + '=([^&]*)(&|$)');
    var urlkey = window.location.search.substr(1).match(regobject);
    if (urlkey !== null) { return decodeURI(urlkey[2]); } else { return null; }
}

export function loadAppURL(url) {
    var iFrame = document.createElement('iframe');
    iFrame.setAttribute('src', url);
    iFrame.setAttribute('style', 'display:none;');
    iFrame.setAttribute('height', '0px');
    iFrame.setAttribute('width', '0px');
    iFrame.setAttribute('frameborder', '0');
    document.body.appendChild(iFrame);
    iFrame.parentNode.removeChild(iFrame);
    iFrame = null;
}

// export const publicUrl = 'http://app.m.bdzg.me/policy-dev';

var shareUrl, shareImg, shareTitle, shareContent;

export function signShareUrl() { axios.post('/signShareUrl?url=' + encodeURIComponent(encodeURIComponent(location.href.split('#')[0]))).then(function(response) { if (response) { wx.config({ debug: false, appId: response.appId, timestamp: response.timestamp, nonceStr: response.nonceStr, signature: response.signature, jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'hideMenuItems'] }); } }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } }); }

export function getShareConfig(fid, mid) {
    axios.get('/getShareConfig/' + fid + '?memberId=' + mid).then(function(response) {
        if (response) {
            shareUrl = response.shareUrl;
            shareImg = response.shareImg;
            shareTitle = response.shareTitle;
            shareContent = response.shareContent;
            weChatShare();
        }
    }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } });
}

export function getAnimationsShareConfig(fid) {
    axios.get('/getAnimationsShareConfig/' + fid).then(function(response) {
        if (response) {
            shareUrl = response.shareUrl;
            shareImg = response.shareImg;
            shareTitle = response.shareTitle;
            shareContent = response.shareContent;
            weChatShare();
        }
    }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } });
}

export function getFamilyExcelShareConfig(_thisVM, fid) {
    axios.get('/getFamilyExcelShareConfig/' + fid).then(function(response) {
        if (response) {
            shareUrl = response.shareUrl;
            shareImg = response.shareImg;
            shareTitle = response.shareTitle;
            shareContent = response.shareContent;
            _thisVM.downloadUrl = response.downloadUrl;
            _thisVM.isNoniOS = (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) ? false : true;
            weChatShare();
        }
    }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } });
}

export function getMemberExcelShareConfig(_thisVM, fid, mid) {
    axios.get('/getMemberExcelShareConfig/' + fid + '?memberId=' + mid).then(function(response) {
        if (response) {
            shareUrl = response.shareUrl;
            shareImg = response.shareImg;
            shareTitle = response.shareTitle;
            shareContent = response.shareContent;
            _thisVM.downloadUrl = response.downloadUrl;
            _thisVM.isNoniOS = (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) ? false : true;
            weChatShare();
        }
    }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } });
}

export function getFamilyBdInfoShare(fid, mid) {
    var _mkey = mid !== '' ? '?memberId=' + mid : '';
    axios.get('/getFamilyBdInfoShare/' + fid + _mkey).then(function(response) {
        if (response) {
            shareUrl = response.shareUrl;
            shareImg = response.shareImg;
            shareTitle = response.shareTitle;
            shareContent = response.shareContent;
            weChatShare();
        }
    }).catch(function(error) { if (error.toString().indexOf('timeout') !== -1) { console.log('网络超时') } });
}

export function weChatShare() {
    wx.ready(function() {
        wx.onMenuShareTimeline({ title: shareTitle + '-' + shareContent, link: shareUrl, imgUrl: shareImg, success: function() {}, cancel: function() {} }); //分享到朋友圈        
        wx.onMenuShareAppMessage({ title: shareTitle, desc: shareContent, link: shareUrl, imgUrl: shareImg, type: 'link', dataUrl: '', success: function() {}, cancel: function() {} }); //分享给朋友        
        wx.onMenuShareQQ({ title: shareTitle, desc: shareContent, link: shareUrl, imgUrl: shareImg, success: function() {}, cancel: function() {} }); //分享到QQ        
        wx.onMenuShareWeibo({ title: shareTitle, desc: shareContent, link: shareUrl, imgUrl: shareImg, success: function() {}, cancel: function() {} }); //分享到腾讯微博        
        wx.onMenuShareQZone({ title: shareTitle, desc: shareContent, link: shareUrl, imgUrl: shareImg, success: function() {}, cancel: function() {} }); //分享到QQ空间
        wx.hideMenuItems({ menuList: ['menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:readMode', 'menuItem:editTag', 'menuItem:favorite', 'menuItem:delete', 'menuItem:originPage', 'menuItem:share:email', 'menuItem:share:brand'] });
    });
}

export function weChatHideMenu() { wx.ready(function() { wx.hideMenuItems({ menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:weiboApp', 'menuItem:share:facebook', 'menuItem:share:QZone', 'menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:readMode', 'menuItem:editTag', 'menuItem:favorite', 'menuItem:delete', 'menuItem:originPage', 'menuItem:share:email', 'menuItem:share:brand'] }); }); }

export function goWindowHref(url) {
    var random = Math.random();
    random = String.prototype.slice.call(random, 2, random.length);
    window.location.href = `${url}?v=${random}`;
}